import json

import sql
from service import EquipmentGroupService
from websocket_information import WebSocketController


def get_groups(ws, uuid):
    try:
        groups = sql.show_groups()
        groups_list = []

        for g in groups:
            devices_id_string = str(g[2])
            devices_ids = devices_id_string.split(',')
            devices_names = ''
            for num in range(len(devices_ids)):
                device_id = devices_ids[num]
                device_name = sql.get_device(device_id)
                if num == 0:
                    devices_names += device_name[1]
                else:
                    devices_names += (',' + device_name[1])
            group_info = {
                'group_id': g[0], 'group_name': g[1], 'groups_ids': g[2], 'groups_names': devices_names
            }
            groups_list.append(group_info)

        article_info = {}
        data = json.loads(json.dumps(article_info))
        data['uuid'] = uuid
        data['message'] = groups_list
        article = json.dumps(data, ensure_ascii=False)
        print(article)
        WebSocketController.on_send(ws, article)
    except Exception as e:
        print(e)


def get_group_by_id(ws, uuid, message):
    try:
        g = sql.show_group_device_ids(message)[0]
        group_info = {'group_id': g[0], 'group_name': g[1], 'devices_ids': g[2]}
        article_info = {}
        data = json.loads(json.dumps(article_info))
        data['uuid'] = uuid
        data['message'] = group_info
        article = json.dumps(data, ensure_ascii=False)
        print(article)
        WebSocketController.on_send(ws, article)
    except Exception as e:
        print(e)


def add_group(ws, uuid, message):
    try:
        group_name = message['group_name']
        group_devices_list = message['group_devices_list']

        article_info = {}
        data = json.loads(json.dumps(article_info))
        data['uuid'] = uuid
        res = sql.add_group(group_name, group_devices_list)
        if res > 0:
            data['message'] = 1
        else:
            data['message'] = -1
        article = json.dumps(data, ensure_ascii=False)
        WebSocketController.on_send(ws, article)
    except Exception as e:
        print(e)


def update_group(ws, uuid, message):
    try:
        group_id = message['group_id']
        group_name = message['group_name']
        group_devices_list = message['group_devices_list']

        article_info = {}
        data = json.loads(json.dumps(article_info))
        data['uuid'] = uuid
        res = sql.update_group(group_id, group_name, group_devices_list)
        if res > 0:
            data['message'] = 1
        else:
            data['message'] = -1
        article = json.dumps(data, ensure_ascii=False)
        WebSocketController.on_send(ws, article)
    except Exception as e:
        print(e)


def delete_group(ws, uuid, message):
    try:
        article_info = {}
        data = json.loads(json.dumps(article_info))
        data['uuid'] = uuid
        res = sql.delete_group(message)
        if res > 0:
            data['message'] = 1
        else:
            data['message'] = -1
        article = json.dumps(data, ensure_ascii=False)
        WebSocketController.on_send(ws, article)
    except Exception as e:
        print(e)
